using UnityEngine;
using System.Collections;

public class ChangePlayerSpeed : MonoBehaviour {
	
	public CharacterMovement _Character;
	public Transform _Current;
	public Renderer _Q;
	public Renderer _E;
	private int count;
	
	void Start()
	{
		if(PlayerPrefs.GetInt("ChangeSpeed") == 0)
		{
			count = 15;
		}
		else
		{
			count = PlayerPrefs.GetInt("ChangeSpeed");
		}
		gameObject.AddComponent("FadeIn");
		gameObject.GetComponent<FadeIn>().createFade(3f);
		
		float pos = 0;
		
		int cur = count-15;
		Debug.Log(cur);
		if(cur > 0)
		{
			pos = _Current.position.x + (0.36f*cur);
			_Current.position = new Vector3(pos,_Current.position.y,_Current.position.z);
		}
		else if(cur < 0)
		{
			pos = _Current.position.x + (0.36f*cur);
			_Current.position = new Vector3(pos,_Current.position.y,_Current.position.z);
		}
		

	}
	
	void Update () 
	{
		if(Input.GetKeyDown(KeyCode.Escape))
		{
			PlayerPrefs.SetFloat("SpeedChar",_Character.speed);
			gameObject.AddComponent("SceneChange");
				
			if(gameObject.GetComponent<SceneChange>().existFade())
			{
				gameObject.GetComponent<SceneChange>().createFade("MainMenu",3);
			}	
			
			PlayerPrefs.SetInt("ChangeSpeed",count);
		}
		
		if(Input.GetKeyDown(KeyCode.E) && count < 29)
		{
			_E.material.color = Color.red;
			count++;
			_Current.position = new Vector3(_Current.position.x+0.36f,_Current.position.y,_Current.position.z);
			_Character.speed += 25f;
		}

        if (Input.touchCount == 2)
        {
            gameObject.AddComponent("SceneChange");

            if (gameObject.GetComponent<SceneChange>().existFade())
            {
                gameObject.GetComponent<SceneChange>().createFade("MainMenu", 3);
            }
        }

        else if (Input.GetKeyUp(KeyCode.E))
        {
            _E.material.color = Color.white;
        }
		
		if(Input.GetKeyDown(KeyCode.Q) && count > 1)
		{
			_Q.material.color = Color.red;
			count --;
			_Current.position = new Vector3(_Current.position.x-0.36f,_Current.position.y,_Current.position.z);
			_Character.speed -= 25f;
		}
		else if(Input.GetKeyUp(KeyCode.Q))
		{
			_Q.material.color = Color.white;
		}

        if (Input.touchCount == 1 && Input.touches[0].phase == TouchPhase.Ended)
        {
            Ray ray = Camera.mainCamera.ScreenPointToRay(new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0f));
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                Debug.Log(hit.transform.name);
                if (hit.transform.name == "_Q" && count > 1)
                {
                    count--;
                    _Current.position = new Vector3(_Current.position.x - 0.36f, _Current.position.y, _Current.position.z);
                    _Character.speed -= 25f;
                }
                if (hit.transform.name == "_E" && count < 29)
                {
                    count++;
                    _Current.position = new Vector3(_Current.position.x + 0.36f, _Current.position.y, _Current.position.z);
                    _Character.speed += 25f;
                }
            }
        }
	}
}
